<?php

namespace App\depository;

use App\Enum\CommonEnum;
use App\Enum\TeamsEnum;
use App\Models\TeamsModel;

class TeamsDep
{
    public $model;

    public function __construct()
    {
        $this->model = new TeamsModel();
    }

    public function get(){
        return $this->model->where("is_del",CommonEnum::NO)->get();
    }

    public function first($id){
        return $this->model->where('id', $id)->first();
    }

    public function list($param)
    {
        $res = $this->model
            ->when(!empty($param['name']), function ($query) use ($param) {
                return $query->where("name", 'like', "%{$param['name']}%");
            })
            ->when(!empty($param['leader_id']), function ($query) use ($param) {
                return $query->where("leader_id",$param['leader_id']);
            })
            ->where("is_del",CommonEnum::NO)
            ->orderBy('id', 'desc')
            ->paginate($param['per_page'], ['*'], 'page', $param['current_page']);;
        return $res;
    }


    public function add($data)
    {
        $res = $this->model->insertGetId($data);
        return $res;
    }

    public function upd($id, $data)
    {
        $res = $this->model->where('id', $id)->update($data);
        return $res;
    }

    public function del($id)
    {
        $res = $this->model->where('id', $id)->update(['is_del' => CommonEnum::YES]);
        return $res;
    }

    public function firstByName($name)
    {
        $res = $this->model->where('name', $name)->where('is_del',CommonEnum::NO)->first();
        return $res;
    }

    public function allOk(){
        $res = $this->model->where('status',TeamsEnum::STATUS_YES)->where('is_del',CommonEnum::NO)->get();
        return $res;
    }

    public function check_exists($gh_id, $gh_secret){
        $res = $this->model->where('status',TeamsEnum::STATUS_YES)->where('id', $gh_id)->where('uuid', $gh_secret)->first();
        return $res;
    }
}
